<?php
require "../model/topicDao.php";
if($_SERVER['REQUEST_METHOD']=='POST'){
    if(isset($_POST['action']) && $_POST['action'] == 'delete') {
        // 删除回复
        $replyId = $_POST['replyId'];
        $result = delete_reply($replyId);
        if($result) {
            echo json_encode(["status" => "success", "message" => "回复已删除"]);
        } else {
            echo json_encode(["status" => "error", "message" => "删除失败"]);
        }
    } else {
        // 添加回复
        $content = $_POST['content'];
        $topicId = $_POST['topicId'];
        $userId = $_POST['userId'];
        $result = add_reply($topicId, $content, $userId);
        if($result) {
            echo json_encode(["status" => "success", "message" => "回复已添加"]);
        } else {
            echo json_encode(["status" => "error", "message" => "回复失败"]);
        }
    }
} else {
    header("location:../views/error.php?msg=非法访问");
}
?>